package com.gopay.scheduler.job;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.common.constants.trans.TxnStaCode;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.constants.wallet.WalletConstants;
import com.gopay.common.cps.dao.order.CpsGeneralMainOrderQueryDAO;
import com.gopay.common.cps.dao.wallet.tyy.WaTyyInviteInfoDao;
import com.gopay.common.cps.dao.wallet.tyy.WaTyyRebateDtlDao;
import com.gopay.common.domain.cps.CpsGenMainOrder;
import com.gopay.common.domain.dps.WaTyyInviteInfo;
import com.gopay.common.domain.dps.WaTyyRebateDtl;
import com.gopay.common.util.DateUtils;
import com.gopay.scheduler.GopayBaseJob;

/**
 * 根据交易完成时间，统计前一天天溢盈理财支付成功的订单，付款人是否是被邀请的用户。若是，则新增邀请人记录。
 * 
 * @author tangyang
 *
 */
public class WaTyyTransRebateJob extends GopayBaseJob {
    private Logger logger = LoggerFactory.getLogger(WaTyyTransRebateJob.class);
    @Resource(name = "cpsGeneralMainOrderDAO")
    private CpsGeneralMainOrderQueryDAO cpsGeneralMainOrderDAO;

    @Resource(name = "waTyyInviteInfoDao")
    private WaTyyInviteInfoDao waTyyInviteInfoDao;
    @Resource(name = "waTyyRebateDtlDao")
    private WaTyyRebateDtlDao waTyyRebateDtlDao;

    @Override
    protected void doJobInternal() {
        // TODO Auto-generated method stub
        logger.info("天溢赢返润统计");
        // 根据清算时间，查询前一日天溢盈理财支付成功订单
        Date currDate = DateUtils.getSystemDate();
        Date yesterday = DateUtils.add(currDate, Calendar.DATE, -1);
        List<CpsGenMainOrder> orderList = cpsGeneralMainOrderDAO
                .findOrderByIntTxnCdAndTxnStaCd(TxnStaCode.TXN_STA_CD_20000.value, IntTxnCd._01615.value, yesterday);
        
        if (orderList != null && orderList.size() > 0) {
            logger.info("天溢赢返润订单数量" + orderList.size());
            for (CpsGenMainOrder order : orderList) {
                logger.info("天溢赢返润订单" + order.toString());
                List<WaTyyRebateDtl> RebateDtls = waTyyRebateDtlDao.findByPurchaseGopayOrderId(order.getGopayOrderId());
                // 判重
                if (RebateDtls != null && RebateDtls.size() > 0) {
                    logger.error("------已存在返润信息：" + RebateDtls.toString());
                    continue;
                }
                // 根据受邀人custId查询WA_TYY_INVITE_INFO信息
                List<WaTyyInviteInfo> InviteList = waTyyInviteInfoDao.findByReceiveCustId(order.getPayCustId());
                if (InviteList == null || InviteList.size() == 0) {
                    continue;
                }
                WaTyyInviteInfo inviteInfo = InviteList.get(0);
                WaTyyRebateDtl rebateDtl = buildRebateDtl(order, inviteInfo);
                // 新增天溢盈分享返润明细表（WA_TYY_REBATE_DTL）记录
                waTyyRebateDtlDao.save(rebateDtl);
            }
        }
    }

    private WaTyyRebateDtl buildRebateDtl(CpsGenMainOrder order, WaTyyInviteInfo inviteInfo) {
        WaTyyRebateDtl rebateDtl = new WaTyyRebateDtl();
        rebateDtl.setRebateId(UUID.randomUUID().toString());
        rebateDtl.setPurchaseGopayOrderId(order.getGopayOrderId());
        rebateDtl.setReceiveCustId(order.getPayCustId());
        rebateDtl.setInviteCustId(inviteInfo.getInviteCustId());
        rebateDtl.setPurchaseAmt(order.getMerTxnAmt());
        BigDecimal rebateAmt = order.getMerTxnAmt().multiply(WalletConstants.TYY_REBATE_SCALE).setScale(2,
                BigDecimal.ROUND_HALF_UP);
        rebateDtl.setRebateAmt(rebateAmt);
        Date currDate = DateUtils.getSystemDate();
        rebateDtl.setPurchaseTs(order.getTranFinishTime());
        rebateDtl.setLastUpdTs(currDate);
        logger.info("------天溢赢返润：" + rebateDtl.toString());
        return rebateDtl;
    }

}
